package com.sunnada.nginx.bean.nginx.http.upstream;

import com.github.odiszapc.nginxparser.NgxParam;
import com.sunnada.nginx.exception.CustonException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.util.List;

@Data
public class Server implements UpstreamItem {
    @ApiModelProperty(hidden = true)
    private String name = "server";
    @ApiModelProperty(value = "服务器地址",required = true)
    private String address;
    @ApiModelProperty(value = "权重")
    private Integer weight;
    @ApiModelProperty(value = "最大连接数")
    private Integer max_conns;
    @ApiModelProperty(value = "最大失败数")
    private Integer max_fails;
    @ApiModelProperty(value = "失败超时时间")
    private Integer fail_timeout;
    @ApiModelProperty(value = "是否为备份服务器")
    private Boolean backup;
    @ApiModelProperty(value = "是否监视上游服务器中的IP地址变化")
    private Boolean resolve;
    @ApiModelProperty(value = "路由名称")
    private String route;
    @ApiModelProperty(value = "服务名称")
    private String service;
    @ApiModelProperty(value = "慢启动时间")
    private Integer slow_start;
    @ApiModelProperty(value = "是否启动\"排放\"模式")
    private Boolean drain;

    public Server(){}

    public Server(String address){
        this.address = address;
    }

    /**
     * 读取配置文件转为Server对象
     * @param list
     */
    public Server(List<String> list){
        this.address = list.get(0);
        for (String s : list) {
            if (s.contains("weight")) this.weight = Integer.parseInt(s.substring(s.indexOf("=")+1));
            if (s.contains("max_conns")) this.max_conns = Integer.parseInt(s.substring(s.indexOf("=")+1));
            if (s.contains("max_fails")) this.max_fails = Integer.parseInt(s.substring(s.indexOf("=")+1));
            if (s.contains("fail_timeout")) this.fail_timeout = Integer.parseInt(s.substring(s.indexOf("=")+1));
            if (s.contains("backup")) this.backup = true;
            if (s.contains("resolve")) this.resolve = true;
            if (s.contains("route")) this.route = s.substring(s.indexOf("=")+1);
            if (s.contains("service")) this.service = s.substring(s.indexOf("=")+1);
            if (s.contains("slow_start")) this.slow_start = Integer.parseInt(s.substring(s.indexOf("=")+1));
            if (s.contains("drain")) this.drain = true;
        }
    }

    public void vali() throws CustonException {
        if (StringUtils.isBlank(address)) throw new CustonException("server中必须包含服务器地址");
    }

    /**
     * 将该对象转为Nginxparse可用的NgxParam对象
     * @return
     */
    public NgxParam toNgxParam(){
        NgxParam ngxParam = new NgxParam();
        ngxParam.addValue("server");
        ngxParam.addValue(address);
        if (weight!=null) ngxParam.addValue("weight="+weight);
        if (max_conns!=null) ngxParam.addValue("max_conns="+max_conns);
        if (max_fails!=null) ngxParam.addValue("max_fails="+max_fails);
        if (fail_timeout!=null) ngxParam.addValue("fail_timeout="+fail_timeout);
        if (backup!=null&&backup) ngxParam.addValue("backup");
        if (resolve!=null&&resolve) ngxParam.addValue("resolve");
        if (StringUtils.isNotBlank(route)) ngxParam.addValue("route="+route);
        if (StringUtils.isNotBlank(service)) ngxParam.addValue("service="+service);
        if (slow_start!=null) ngxParam.addValue("slow_start="+slow_start);
        if (drain!=null&&drain) ngxParam.addValue("drain");
        return ngxParam;
    }
}
